/**
 * Created by gm on 2016/7/28.
 */
/**
 * Created by k618 on 2016/7/13.
 */
var express=require('express');
var router=express.Router();

var monk=require('monk');
var db=monk('localhost:27017/usercollection');
var collection=db.get('usercollection');

router.get('/',function(req,res){
    collection.find({},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.post('/',function(req,res){
    if(req.body!=null){
        if(req.body.name=="addClient"){
            collection.findOne({"$or":[{appId:req.body.appId},{secretKey:req.body.secretKey}]},function(err,data){
                if(err) throw err;
                if(data==null){
                    collection.insert({
                        appId:req.body.appId,
                        secretKey:req.body.secretKey,
                        status:req.body.status,
                        checkContent:req.body.checkContent,
                        remark:req.body.remark
                    },function(err,result){
                        if(err) throw err;
                        res.json(data);
                    });
                }else{
                    if(data.appId==req.body.appId){
                        res.json({errNum:0,message:"商户号重复，请重新输入"});
                    }else if(data.secretKey==req.body.secretKey){
                        res.json({errNum:1,message:"密钥重复，请重新生成"});
                    }
                }
            });
        }
        var pageSize=parseInt(req.body.pageSize);
        var skip=parseInt((req.body.p_current-1)*pageSize);
        if(req.body.name=="scanClient"){
            collection.count({},function(err,data){
                if(err) throw err;
                collection.find({},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=="searchClient"){
            var con=new RegExp(req.body.content);
            collection.count({"$or":[{appId:con},{remark:con}]},function(err,data){
                if(err) throw err;
                collection.find({"$or":[{appId:con},{remark:con}]},{sort:{_id:-1},skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=="editClient"){
            collection.findOne({appId:{$nin:[req.body.appId]},secretKey:req.body.secretKey},function(err,data){
                if(err) throw err;
                if(data==null){
                    collection.update({
                            _id:req.body._id
                        },
                        {$set:
                        {secretKey:req.body.secretKey,
                            status:req.body.status,
                            checkContent:req.body.checkContent,
                            remark:req.body.remark}
                        },function(err,result){
                            if(err) throw err;
                            res.json(data);
                        });
                }else{
                    res.json({errNum:1,message:"密钥重复，请重新生成"});
                }
            });
        }
        if(req.body.name=="usableClient"){
            collection.count({status:"1"},function(err,data){
                if(err) throw err;
                collection.find({status:"1"},{skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=="expiredClient"){
            collection.count({status:"2"},function(err,data){
                if(err) throw err;
                collection.find({status:"2"},{skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
        if(req.body.name=="unusedClient"){
            collection.count({status:"0"},function(err,data){
                if(err) throw err;
                collection.find({status:"0"},{skip:skip,limit:pageSize},function(err,result){
                    if(err) throw err;
                    var jsonArray={count:data,result:result};
                    res.json(jsonArray);
                });
            });
        }
    }
});

router.get('/user/:id',function(req,res){
    collection.findOne({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.put('/:id',function(req,res){
    collection.update({
            _id:req.body._id
        },
        {$set:
        {secretKey:req.body.secretKey,
            status:req.body.status,
            checkContent:req.body.checkContent,
            remark:req.body.remark}
        },function(err,result){
            if(err) throw err;
            res.json(result);
        });
});

router.delete('/:id',function(req,res){
    collection.remove({_id:req.params.id},function(err,data){
        if(err) throw err;
        res.json(data);
    });
});

router.get('/userInfo',function(req,res){
   collection.findOne({appId:req.query.appId},function(err,data){
       if(err) throw err;
       console.log(data);
       res.json(data);
   });
});

router.get('/getappids',function(req,res){
  collection.find({},{fields:{_id:0,appId:1}},function(err,data){
    if(err) throw err;
    res.json({result:data});
  })
});


exports= module.exports=router;